package com.file.system.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebLog;
import com.file.system.service.system.IWebLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;


/**
 * 日志
 *
 * @author jch
 */
@RestController
@RequestMapping("/web/log")
public class WebLogController {
    @Autowired
    private IWebLogService service;


    /**
     * 查询日志分页
     *
     * @param webLog 日志
     * @return CommonResult
     * @apiNote -
     * @author 靳春晖
     */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<WebLog>>> select(@PathVariable
                                                       @Min(value = 1, message = "页数最小1")
                                                       Integer pageIndex,
                                                       @PathVariable
                                                       @Min(value = 1, message = "每页条数最小1")
                                                       @Max(value = 100, message = "每页条数最大100")
                                                       Integer pageSize,
                                                       @RequestBody @Validated WebLog webLog) {
        QueryWrapper<WebLog> query = new QueryWrapper<>();
        query.orderByDesc("a.create_time");
        //根据主键id查询
        if (isNotEmpty(webLog.getFkObjectId())) query.eq("a.fk_object_id", webLog.getFkObjectId());
        //查询集合
        DataPage<List<WebLog>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }


    /**
     * 通过id查询日志
     *
     * @param id 字典id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id}")
    public CommonResult<WebLog> getById(@PathVariable
                                           @Min(value = 1, message = "id长度最小1")
                                           Long id) {
        //查询集合
        WebLog webLog = service.getById(id);
        return CommonResult.ok().setResult(webLog);
    }
}
